﻿using OxyPlot;
using OxyPlot.Axes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Zhaoxi.WinStudyBoardSystem
{
    public partial class FrmMain : Form
    {   
        SerialPort serialPort;//串口对象

        public FrmMain()
        {
            InitializeComponent();
            serialPort = new SerialPort();
            //串口下拉框
            RefreshPortNmaes();
            //波特率
            InitBaudRates();
            //校验位
            InitParitys();
            //数据位
            InitDataBits();
            //停止位
            InitStopBits();
        }

        /// <summary>
        /// 获取系统可用的串口列表，并显示在对应的下拉框中
        /// </summary>
        private void RefreshPortNmaes()
        {
            cboPorts.Items.Clear();//清空下拉框中的所以项
            var portList = SerialPort.GetPortNames();
            foreach (var portName in portList)
            {
                cboPorts.Items.Add(portName);
            }

            if (cboPorts.Items.Count > 0)
            {
                cboPorts.SelectedIndex = 0;//默认选择第一个端口
            }
        }
        /// <summary>
        /// 刷新端口列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshPortNmaes();
        }

        /// <summary>
        /// 加载波特率下拉框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitBaudRates()
        {
            int[] baudrates = new int[]
            {
                4800,
                9600,
                14400,
                19200,
                38400,
                57600,
                115200,
                128000
            };
            foreach(var rate in baudrates)
            {
                cboBaudRates.Items.Add(rate);
            }
            cboBaudRates.SelectedItem = 9600;
        }

        /// <summary>
        /// 初始化校验位下拉框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitParitys()
        {
            foreach (var parity in Enum.GetNames(typeof(Parity)))
            {
                cboParitys.Items.Add(parity);
            }
            cboParitys.SelectedItem = "None";
        }

        /// <summary>
        /// 初始化数据位下拉框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitDataBits()
        {
            int[] databits = new int[] { 5, 6, 7, 8 };
            foreach (var data in databits)
            {
                cboDatabits.Items.Add(data);
            }
            cboDatabits.SelectedItem = 8;
        }

        /// <summary>
        /// 初始化停止位下拉框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitStopBits()
        {
            foreach(var stopBits in Enum.GetNames(typeof(StopBits)))
            {
                cboStopbits.Items.Add(stopBits);
            }
            cboStopbits.SelectedItem = "One";
        }

        /// <summary>
        /// 页面信息初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //页面信息初始化
            InitFormInfo();
            //图表初始化
            InitPlots();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitFormInfo()
        {
            //仪表值重置
            umTem.Value = 0;
            umHum.Value = 0;
            umBright.Value = 0;

            //历史值重置，模拟状态初始化为false
            lblTem_H.Text = "0.0";
            lblTem_L.Text = "0.0";
            chkStart_T.Checked = false;

            lblHum_H.Text = "0";
            lblHum_L.Text = "0";
            chkStart_H.Checked = false;

            lblBring_H.Text = "0";
            lblBring_L.Text = "0";
            chkStart_B.Checked = false;

            //曲线显示状态默认true
            chkBright.Checked = true;
            chkHum.Checked = true;
            chkTemp.Checked = true;

            //显示屏初始化
            lblShowMsg.Text = "  ";
            txtSendMsg.Text = "  ";

            //状态显示区域
            chkState_01.Checked = false;
            chkState_02.Checked = false;
            chkState_03.Checked = false;
            chkState_04.Checked = false;
            chkState_05.Checked = false;
            chkState_all.Checked = false;
        }

        PlotModel plotModel = null;
        DateTimeAxis dateTimeAxis;//x轴
        LinearAxis valueAxis;//y轴

        /// <summary>
        /// 图表初始化
        /// </summary>
        private void InitPlots()
        {
            plotModel = new PlotModel()
            {
                IsLegendVisible = false,
                PlotAreaBorderColor = OxyColors.LightGray,
                PlotAreaBackground = OxyColors.White,
            };

            dateTimeAxis = new DateTimeAxis()
            {
                MajorGridlineStyle = LineStyle.Dash,
                MajorGridlineThickness = 0,
                IntervalLength = 5,
                IntervalType = DateTimeIntervalType.Seconds,
                FontSize = 10,
                TextColor = OxyColors.Gray,
                IsPanEnabled = false,
                IsZoomEnabled = false,
                AxislineColor = OxyColors.LightGray,
                StringFormat ="MM:ss"
            };
            plotModel.Axes.Add(dateTimeAxis);

            //y
            valueAxis = new LinearAxis()
            {
                MajorGridlineStyle = LineStyle.Dash,
                IntervalLength = 20,
                IsZoomEnabled = false,
                IsPanEnabled = false,
                Maximum = 110,
                Minimum = -20,
                FontSize = 10,
                TextColor = OxyColors.Gray,
                AxislineColor = OxyColors.LightGray
            };
            plotModel.Axes.Add(valueAxis);



            pvTrends.Model = plotModel;
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.LightGray,1,1,this.Width-2,this.Height-2);//绘制边框
        }

        private void uMeter2_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.LightGray, 0, 0, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2);  //绘制边框
        }

        #region 窗口拖动
        Point mPoint = new Point();
        bool isMove = false;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = e.Location;//当前按下的坐标
            isMove = true;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(isMove&&e.Button==MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isMove = false;
        }
        #endregion

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label20_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }




    }
}
